package org.jeecg.modules.clue.job;


import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.clue.service.IClueTaskQueryLogService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

@Component
@Slf4j
@DisallowConcurrentExecution
public class ClueBatchQueryListener implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    public static String Topic = "ClueBatchQuery";

    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    IClueTaskQueryLogService clueTaskQueryLogService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("ClueBatchQueryListener 定时任务");

        ZSetOperations<String, String> opsForZSet = stringRedisTemplate.opsForZSet();
        Set<String> batchNos = opsForZSet.rangeByScore(Topic, 0, System.currentTimeMillis());

        if (CollectionUtil.isEmpty(batchNos)) {
            return;
        }
        log.info("ClueBatchQueryListener SCHEDULE {} CONSUME {}", Topic, batchNos);
        for (String batchNo : batchNos) {
            //  查询任务
            clueTaskQueryLogService.queryClueBatchNo(batchNo);
            //   删除队列value
            opsForZSet.remove(Topic, batchNo);


        }

    }
}
